import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.*;
public class Zipper {

	public void zipDir(String dirName, String zipName){
		ZipOutputStream zip = null;
        FileOutputStream fW = null;
        try{
        fW = new FileOutputStream(zipName);
        zip = new ZipOutputStream(fW);
      
        addFolderToZip("", dirName, zip);
        zip.close();
        fW.close();
        }
       catch(IOException e){
    	   e.printStackTrace();
       }
        
       
	}
	private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) throws IOException {
		File folder = new File(srcFolder);
		for (String fileName : folder.list()) {
            if (path.equals("")) {
                addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip);
            } 
            else {
                 addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip);
            }
        }
	}
	private void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws IOException {
        File folder = new File(srcFile);
        
            if (folder.isDirectory()) {
                addFolderToZip(path, srcFile, zip);
            }
            else {
                byte[] buf = new byte[1024];
                int len;
                FileInputStream in = new FileInputStream(srcFile);
                zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
                while ((len = in.read(buf)) > 0) {
                    zip.write(buf, 0, len);
                }
            }
        
    }
		 
}